<template>
  <div class="wrapper">
    <JForm :form-config="dynamicValidateForm" @handleBoxEvent="handleBoxEvent" />
  </div>
</template>

<script>
import JForm from '@/components/JForm'
import { getDictListByType } from '@/api/dictionaries'

export default {
  name: 'AreaUpdate',
  components: { JForm },
  props: {
    selectedItem: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    const userInfo = [
      {
        name: 'cityType',
        label: '城市类型',
        type: 'option',
        required: true,
        config: {}
      },
      {
        name: 'code',
        label: '编码',
        type: 'string',
        required: true
      },
      {
        name: 'countryCode',
        label: '国家编码',
        type: 'string',
        required: false,
        placeholder: '国家编码'
      },
      {
        name: 'fullname',
        label: '全称',
        type: 'string',
        required: false,
        placeholder: '全称'
      },
      {
        name: 'lat',
        label: '纬度',
        type: 'string',
        required: false,
        placeholder: ''
      },
      {
        name: 'lng',
        label: '经度',
        type: 'string',
        required: false
      },
      {
        name: 'name',
        label: '名称',
        type: 'string',
        required: false
      },
      {
        name: 'note',
        label: '备注',
        type: 'string',
        required: false
      },
      {
        name: 'pcode',
        label: '父级编码',
        type: 'string',
        required: false
      },
      {
        name: 'rankd',
        label: '级别',
        type: 'string',
        required: false
      },
      {
        name: 'sortNum',
        label: '排序',
        type: 'string',
        required: false
      }
    ]
    return {
      // dynamicValidateForm: userInfo,
      userInfo,
      CITY_TYPE: []
    }
  },
  computed: {
    dynamicValidateForm() {
      const data = this.userInfo
      const values = this.selectedItem
      const result = []
      data.forEach(i => {
        const { type, name } = i
        if (type === 'option') {
          i.config.options = this.filterOption(i)
        }
        i.value = values[name]
        result.push(i)
      })
      return result
    },
    cityTypeOption() {
      const data = this.CITY_TYPE
      data.forEach(item => {
        const { code, name } = item
        item.value = code
        item.label = name
      })
      return data
    }
  },
  watch: {},
  created() {
    this.getDicCITY_TYPE()
  },
  mounted() {},
  methods: {
    getDicCITY_TYPE() {
      getDictListByType({ dictTypeCode: 'CITY_TYPE' }).then(res => {
        const { code, data /* message */ } = res
        if (code === 200) { this.CITY_TYPE = data }
      })
    },
    handleBoxEvent(emitObj) {
      this.submitForm(emitObj)
    },
    filterOption(item) {
      const key = item.name
      return this[`${key}Option`]
    },
    submitForm(emitObj) {
      this.$emit('handleBoxEvent', { target: emitObj })
    }
  }
}
</script>
<style lang="scss" scoped>
.wrapper{
   padding: 0px;
   margin: 0px;
}
</style>
